'use strict';

module.exports = app => {
    return class PostService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 获取帖子列表 分页查询
         * typeName： 1. 普通贴 2. 提问帖
         * @param id
         * @returns {Promise<void>}
         */
        async list({ id, pageNo, typeName = 1, orderBy = -1 }) {
            let url = this.app.urls('getPost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumId: id,
                    typeName,
                    orderBy,
                    pageNo
                }
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 热门帖子
         * @param id
         * @param type
         * @returns {Promise<void>}
         */
        async hot({ id, pageNo, pageSize }) {
            let url = this.app.urls('getHotPost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumId: id,
                    pageNo,
                    pageSize
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async getAnswer({ id, pageNo, pageSize }) {
            let url = this.app.urls('getAnswer');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumId: id,
                    pageNo,
                    pageSize
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 帖子详情
         * @param id
         * @returns {Promise<void>}
         */
        async detail({ id }) {
            let url = this.app.urls('getPostDetail');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 发布帖子
         * @param forumId
         * @param content
         * @param typeName
         * @param title
         * @param intro
         * @param images
         * @returns {Promise<void>}
         */
        async add({ forumId, content, typeName, title, intro, images}) {
            let url = this.app.urls('sendPost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    forumId,
                    content,
                    typeName,
                    title,
                    intro,
                    images
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


    }

};
